package com.desmao.mapper.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.desmao.mapper.BaseMapper;
import com.desmao.mapper.enums.RdbOperation;
import com.desmao.util.ReflectUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("baseMapper")
public class BaseMapperImpl<T, ID extends Serializable> extends SqlSessionDaoSupport
        implements BaseMapper<T, ID>, Serializable {

    private static final long serialVersionUID = 7623507504198633838L;

    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    public String getNamespace() {
        Class clazz = ReflectUtils.getGenericInterface(getClass(), 0);
        String nameSpace = clazz.getName();
        return nameSpace;
    }

    @Override
    public int insert(T entity) {
        return getSqlSession().insert(getNamespace() + RdbOperation.INSERT.value(), entity);
    }

    @Override
    public int insertSelective(T entity) {
        return getSqlSession().insert(getNamespace() + RdbOperation.INSERT_SELECTIVE.value(), entity);
    }

    @Override
    public T selectByPrimaryKey(ID id) {
        return getSqlSession().selectOne(getNamespace() + RdbOperation.SELECT_BY_PRIMARY_KEY.value(),id);
    }

    @Override
    public int updateByPrimaryKeySelective(T entity) {
        return getSqlSession().update(getNamespace() + RdbOperation.UPDATE_BY_PRIMARY_KEY_SELECTIVE.value(), entity);
    }

    @Override
    public int updateByPrimaryKey(T entity) {
        return getSqlSession().update(getNamespace() + RdbOperation.UPDATE_BY_PRIMARY_KEY.value(), entity);
    }

    @Override
    public int deleteByPrimaryKey(ID id) {
        return getSqlSession().delete(getNamespace() + RdbOperation.DELETE_BY_PRIMARY_KEY.value(), id);
    }

    public PageInfo<T> selectPage(int page, int rows) {
        return this.selectPage(null, page, rows, null);
    }

    @Override
    public List<T> selectList() {
        return this.selectList(null, null);
    }

    @Override
    public List<T> selectList(Map<String, Object> condition) {
        return this.selectList(condition, null);
    }


    @Override
    public List<T> selectList(Map<String, Object> condition, String orderBy) {
        if (condition == null ) {
            condition = new HashMap<>();
        }
        condition.put("orderBy", orderBy);
        return getSqlSession().selectList(getNamespace() + RdbOperation.SELECT_LIST.value(), condition);
    }

    @Override
    public PageInfo<T> selectPage(Map<String, Object> condition, int page, int rows, String orderBy) {
        //获取总数,初始化pagebean
        PageHelper.startPage(page, rows, true);

        if (condition == null) {
            condition = new HashMap<String, Object>();
        }
        condition.put("orderBy", orderBy);

        List<T> list = this.getSqlSession().selectList(
                getNamespace() + RdbOperation.SELECT_ALL.value(), condition);
        return new PageInfo<T>(list);

    }

    @Override
    public Class<T> getEntityClass() {
        return ReflectUtils.getClassGenricType(getClass());
    }

}
